/**
* Copyright 2012 nabla
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.nabla.wapp.client.model.field;

import com.nabla.wapp.client.general.Assert;
import com.nabla.wapp.client.model.Model;
import com.smartgwt.client.widgets.form.fields.SelectItem;

public class SelectBoxField extends TextField {

	private Model				model;
	private final SelectItem	editor = new SelectItem();

	public SelectBoxField(final String name) {
		super(name);
		setEditorType(editor);
	}

	public SelectBoxField(final String name, final Model model, final String valueField, final String displayField, final FieldAttributes... attributes) {
		super(name);
		setModel(model, valueField, displayField);
		FieldAttributes.applyAll(this, attributes);
	}

	public SelectBoxField(final String name, final Model model, final FieldAttributes... attributes) {
		super(name);
		setModel(model);
		FieldAttributes.applyAll(this, attributes);
	}

	public void setModel(final Model model, final String valueField, final String displayField) {
		Assert.notNull(model.getField(valueField));
		Assert.notNull(model.getField(displayField));

		this.model = model;
		editor.setOptionDataSource(model);
		editor.setValueField(valueField);
		editor.setDisplayField(displayField);
		setEditorType(editor);
	}

	public void setModel(final Model model) {
		this.model = model;
		editor.setOptionDataSource(model);
		setEditorType(editor);
	}

	public Model getModel() {
		return model;
	}

	public String getValueField() {
		return editor.getValueField();
	}

	public String getDisplayField() {
		return editor.getDisplayField();
	}

}
